package com.example.administrator.funchat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.funchat.model.Chat_information;
import com.example.administrator.funchat.R;

import org.xutils.x;

import java.util.List;

/**
 * 聊天界面适配器
 * Created by Administrator on 2016/6/24.
 */
public class ChatAdapter extends ArrayAdapter<Chat_information> {

    private int resourceId;

    public ChatAdapter(Context context, int resource, List<Chat_information> objects) {
        super(context, resource, objects);
        resourceId = resource;

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Chat_information information = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
            viewHolder.rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
            viewHolder.leftMsg = (TextView) view.findViewById(R.id.left_imag);
            viewHolder.rightMsg = (TextView) view.findViewById(R.id.right_imag);
            viewHolder.leftImag = (ImageView) view.findViewById(R.id.left_head);
            viewHolder.rightImag = (ImageView) view.findViewById(R.id.right_head);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        if (information.getType() == information.TYPE_RECEIVED) {
            viewHolder.leftLayout.setVisibility(View.VISIBLE);
            viewHolder.rightLayout.setVisibility(View.GONE);
            viewHolder.leftMsg.setText(information.getContent());
            //viewHolder.leftImag.setImageResource(information.getImage_left());
            x.image().bind(viewHolder.leftImag,information.getImage_left());
        } else if (information.getType() == information.TYPE_SENT) {
            viewHolder.rightLayout.setVisibility(View.VISIBLE);
            viewHolder.leftLayout.setVisibility(View.GONE);
            viewHolder.rightMsg.setText(information.getContent());
            //viewHolder.rightImag.setImageResource(information.getImage_right());
            x.image().bind(viewHolder.rightImag,information.getImage_right());
        }
        return view;

    }

    class ViewHolder {

        LinearLayout leftLayout;

        LinearLayout rightLayout;

        TextView leftMsg;

        TextView rightMsg;

        ImageView leftImag;

        ImageView rightImag;
    }

}
